package com.dsj.dao;

import com.dsj.entity.CustomerEntity;
import com.dsj.util.DBConnection;
import com.dsj.util.Encrypt;

import javax.xml.stream.FactoryConfigurationError;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * 会员表操作类，负责对该表的存取操作
 */
public class CustomerDao {
    private Connection connection = null;
    private PreparedStatement ps = null;
    private DBConnection jdbc = null;

    public CustomerDao() {
        jdbc = new DBConnection();
        connection = jdbc.connetction;
    }

    /**
     * 以会员名称为条件查询会员信息，并返回查询得到会员实体
     */
    public CustomerEntity selectCustomerEntity(String userName) {
        CustomerEntity user = null;
        try {
            String sql = "select * from tb_customer where user_name =?";
            ps = connection.prepareStatement(sql);
            ps.setString(1, userName);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                user = new CustomerEntity();
                user.setId(rs.getInt("id"));
                user.setUserName(rs.getString("user_name"));
                user.setPassword(rs.getString("password"));
                user.setRealName(rs.getString("real_name"));
                user.setMobile(rs.getString("mobile"));
                user.setEmail(rs.getString("email"));
                user.setPasswordQuestion(rs.getString("password_question"));
                user.setPasswordHintAnswer(rs.getString("password_hint_answer"));
            }
        } catch (Exception e) {
            System.out.println("查询会员信息失败");
        }
        return user;
    }

    /**
     * 以密码和id为参数更新会员密码
     */
    public boolean updateCustomerPassword(String password, int id) {
        boolean flag = false;
        try {
            String sql = "update tb_customer set password =? where id =?";
            ps = connection.prepareStatement(sql);
            ps.setString(1, password);
            ps.setInt(2, id);
            int i = ps.executeUpdate();
            if (i > 0) {
                flag = true;
            }
        } catch (Exception e) {
            System.out.println("更新会员密码失败");
        }
        return flag;
    }

    /**
     * 新增会员
     */
    public boolean insertCustomer(CustomerEntity user) {
        boolean flag = false;
        try {
            String sql = "insert into tb_customer(user_name,password,real_name,mobile,email,password_question,password_hint_answer) values(?,?,?,?,?,?,?)";
            ps = connection.prepareStatement(sql);
            ps.setString(1, user.getUserName());
            ps.setString(2, user.getPassword());
            ps.setString(3, user.getRealName());
            ps.setString(4, user.getMobile());
            ps.setString(5, user.getEmail());
            ps.setString(6, user.getPasswordQuestion());
            ps.setString(7, user.getPasswordHintAnswer());
            int i = ps.executeUpdate();
            if (i > 0) {
                flag = true;
            }
        } catch (Exception e) {
            System.out.println("新增会员失败");
        }
        return flag;
    }

    /**
     * 查询数据库是否存在相同信息
     * @param name  查询的字段名称
     * @param value 查询的字段值
     * @return true 存在相同信息，false 不存在相同信息
     */
    public boolean hasSameValue(String name, String value) {
        boolean flag = false;
        try {
            String sql = "select * from tb_customer where " + name + " =?";
            ps = connection.prepareStatement(sql);
            ps.setString(1, value);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                flag = true;
            }
        } catch (Exception e) {
            System.out.println("查询数据库是否存在相同信息失败");
        }
        return flag;
    }


    /**
     * 更新会员信息
     * @param user 会员实体对象
     * @return true 更新成功，false 更新失败
     */
    public boolean updateCustomer(CustomerEntity user) {
        boolean flag = false;
        try {
                // 对用户密码进行加密
                String password = Encrypt.encryptPassword(user.getPassword());
                String sql = "update tb_customer set user_name =?,real_name =?,password =?,mobile =?,email =?,password_question =?,password_hint_answer =? where id =?";
                // 准备执行SQL查询或更新操作的PreparedStatement对象
                ps = connection.prepareStatement(sql);
                ps.setString(1, user.getUserName());
                ps.setString(2, user.getRealName());
                ps.setString(3, password);
                ps.setString(4, user.getMobile());
                ps.setString(5, user.getEmail());
                ps.setString(6, user.getPasswordQuestion());
                ps.setString(7, user.getPasswordHintAnswer());
                ps.setInt(8, user.getId());
                int i = ps.executeUpdate();
                if (i > 0) {
                    flag = true;
                }
                // 关闭预处理语句
                ps.close();
                // 关闭数据库连接
                connection.close();
                // 将jdbc连接对象置为null
                jdbc.connetction = null;
            } catch (Exception e) {
                System.out.println("更新会员信息失败");
            }
        return flag;
    }

}
